package com.servlet.user;

import com.enitiy.User;
import com.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * 创建作者 ：MySunshine
 * 创建时间 2022/10/25 9:56
 * 项目名称 SHUSHAN
 */
@WebServlet("/userReg")//用户注册
public class UserReg extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        String age = req.getParameter("age");
        String sex = req.getParameter("sex");
        String kid = req.getParameter("kid");
        String phone = req.getParameter("phone");
        int num =(int)(Math.random()*900000000)+100000000;//账号生成
        Date nowDate = new Date();
        long time = nowDate.getTime();
        java.sql.Date date=new java.sql.Date(time);
        System.out.println(phone);
        System.out.println(date);
        System.out.println(name+pwd+age+sex+kid+num);
        UserServiceImpl usi = new UserServiceImpl();
       //验证码
        String inputCode = req.getParameter("inputCode");
        String code = (String) req.getSession().getAttribute("code");
        if (inputCode.equals(code)){
            System.out.println("验证码正确");
            boolean b = usi.UserReg(new User(num, name, pwd, Integer.parseInt(age),sex,phone, date, Integer.parseInt(kid)));
            if(b){
                System.out.println("注册成功！");
                req.setAttribute("num",num);
                req.getRequestDispatcher("/page/user/userAvatar.jsp").forward(req,resp);

            }else {
                System.out.println("注册失败！");
                req.getRequestDispatcher("/page/user/userReg.jsp").forward(req,resp);
            }
        }else {
            System.out.println("验证码错误");
        }

    }
}
